//      preferences.cpp
//
//      Copyright 2011 David Vachulka <david@konstrukce-cad.com>
//
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 3 of the License, or
//      (at your option) any later version.
//
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.


#include "defs.h"
#include "config.h"
#include "utils.h"
#include "i18n.h"
#include "preferences.h"

Preferences::Preferences() :
    m_oneInstance(FALSE),
    m_disableSortTabs(FALSE),
    m_reconnect(FALSE),
    m_numberAttempt(1),
    m_delayAttempt(20),
    m_maxAway(200),
#ifdef HAVE_TRAY
    m_useTray(TRUE),
#else
    m_useTray(FALSE),
#endif //HAVE_TRAY
    m_closeToTray(m_useTray?TRUE:FALSE),
    m_useSpell(FALSE),
    m_ownServerWindow(TRUE),
    m_logging(FALSE),
    m_logPath(""),
    m_autoload(FALSE),
    m_autoloadPath(""),
    m_unreadColor(FXRGB(0,0,255)),
    m_highlightColor(FXRGB(255,0,0)),
    m_tabPosition(0),
    m_font("Helvetica,90,normal,regular"),
    m_ircFont(""),
    m_nickCompletionChar(":"),
    m_sameCmd(FALSE),
    m_sameList(FALSE),
    m_coloredNick(TRUE),
    m_stripColors(TRUE),
    m_loadLog(FALSE),
    m_loadLines(25),
    m_themePath(DXIRC_DATADIR PATHSEPSTRING "icons" PATHSEPSTRING "default"),
    m_themesList(FXString(m_themePath+";")),
    m_sortUserMode(FALSE),
    m_ignoreCommandsList(""),
    m_dccPath(FXSystem::getHomeDirectory()),
    m_dccIP(""),
    m_dccPortD(0),
    m_dccPortH(0),
    m_dccTimeout(66),
    m_autoDccChat(FALSE),
    m_autoDccFile(FALSE),
    m_sounds(FALSE),
    m_soundConnect(FALSE),
    m_pathConnect(DXIRC_DATADIR PATHSEPSTRING "sounds" PATHSEPSTRING "connected.wav"),
    m_soundDisconnect(FALSE),
    m_pathDisconnect(DXIRC_DATADIR PATHSEPSTRING "sounds" PATHSEPSTRING "disconnected.wav"),
    m_soundMessage(FALSE),
    m_pathMessage(DXIRC_DATADIR PATHSEPSTRING "sounds" PATHSEPSTRING "message.wav"),
    m_notify(FALSE),
    m_notifyPosition(3),
    m_notifyConnect(FALSE),
    m_notifyDisconnect(FALSE),
    m_notifyMessage(FALSE),
    m_useSmileys(FALSE),
    m_usersShown(TRUE),
    m_statusShown(TRUE),
    m_x(50),
    m_y(50),
    m_w(888),
    m_h(666),
    m_maximized(FALSE),
    m_showSpellCombo(TRUE)
{
    m_appTheme.base = FXRGB(212,208,200);
    m_appTheme.back = FXRGB(255,255,255);
    m_appTheme.border = FXRGB(0,0,0);
    m_appTheme.fore = FXRGB(0,0,0);
    m_appTheme.menuback = FXRGB(10,36,106);
    m_appTheme.menufore = FXRGB(255,255,255);
    m_appTheme.selback = FXRGB(10,36,106);
    m_appTheme.selfore = FXRGB(255,255,255);
    m_appTheme.tipback = FXRGB(255,255,225);
    m_appTheme.tipfore = FXRGB(0,0,0);
    m_appTheme.notifyback = FXRGB(255,255,225);
    m_appTheme.notifyfore = FXRGB(0,0,0);
    m_appTheme.hilite = makeHiliteColor(m_appTheme.base);
    m_appTheme.shadow = makeShadowColor(m_appTheme.base);
    m_trayColor = m_appTheme.base;
    m_colors.text = FXRGB(255,255,255);
    m_colors.back = FXRGB(0,0,0);
    m_colors.user = FXRGB(43,57,35);
    m_colors.action = FXRGB(255,165,0);
    m_colors.notice = FXRGB(0,163,215);
    m_colors.error = FXRGB(255,0,0);
    m_colors.hilight = FXRGB(11,154,11);
    m_colors.link = FXRGB(142,196,210);
    m_colors.mymsg = FXRGB(130,150,253);
    m_colors.log = FXRGB(43,57,35);
    fillDefaultKeybindings();
}

void Preferences::load()
{
    FXSettings set;
    set.parseFile(utils::instance().getIniFile(), TRUE);

    //General settings pane in Configdialog
    m_oneInstance = set.readBoolEntry("SETTINGS", "oneinstance", m_oneInstance);
    m_disableSortTabs = set.readBoolEntry("SETTINGS", "disableSortTab", m_disableSortTabs);
    m_reconnect = set.readBoolEntry("SETTINGS", "reconnect", m_reconnect);
    m_numberAttempt = set.readIntEntry("SETTINGS", "numberAttempt", m_numberAttempt);
    m_delayAttempt = set.readIntEntry("SETTINGS", "delayAttempt", m_delayAttempt);
    m_maxAway = set.readIntEntry("SETTINGS", "maxAway", m_maxAway);
    m_useTray = set.readBoolEntry("SETTINGS", "tray", m_useTray);
    m_closeToTray = set.readBoolEntry("SETTINGS", "closeToTray", m_closeToTray);
    m_useSpell = set.readBoolEntry("SETTINGS", "useSpell", m_useSpell);
    if(!utils::instance().getAvailableSpellLangsNum()) m_useSpell = FALSE;
    m_ownServerWindow = set.readBoolEntry("SETTINGS", "serverWindow", m_ownServerWindow);
    m_logging = set.readBoolEntry("SETTINGS", "logging", m_logging);
    m_logPath = set.readStringEntry("SETTINGS", "logPath", m_logPath.text());
    if(m_logging && !FXStat::exists(m_logPath)) m_logging = FALSE;
    m_autoload = set.readBoolEntry("SETTINGS", "autoload", m_autoload);
    m_autoloadPath = set.readStringEntry("SETTINGS", "autoloadPath");
    if(m_autoload && !FXStat::exists(utils::isUtf8(m_autoloadPath.text(), m_autoloadPath.length()) ? m_autoloadPath : utils::instance().localeToUtf8(m_autoloadPath))) m_autoload = FALSE;

    //Look settings pane in Configdialog
    m_appTheme.base = set.readColorEntry("SETTINGS", "basecolor", m_appTheme.base);
    m_appTheme.back = set.readColorEntry("SETTINGS", "backcolor", m_appTheme.back);
    m_appTheme.border = set.readColorEntry("SETTINGS", "bordercolor", m_appTheme.border);
    m_appTheme.fore = set.readColorEntry("SETTINGS", "forecolor", m_appTheme.fore);
    m_appTheme.menuback = set.readColorEntry("SETTINGS", "selmenubackcolor", m_appTheme.menuback);
    m_appTheme.menufore = set.readColorEntry("SETTINGS", "selmenutextcolor", m_appTheme.menufore);
    m_appTheme.selback = set.readColorEntry("SETTINGS", "selbackcolor", m_appTheme.selback);
    m_appTheme.selfore = set.readColorEntry("SETTINGS", "selforecolor", m_appTheme.selfore);
    m_appTheme.tipback = set.readColorEntry("SETTINGS", "tipbackcolor", m_appTheme.tipback);
    m_appTheme.tipfore = set.readColorEntry("SETTINGS", "tipforecolor", m_appTheme.tipfore);
    m_appTheme.notifyback = set.readColorEntry("SETTINGS", "notifybackcolor", m_appTheme.notifyback);
    m_appTheme.notifyfore = set.readColorEntry("SETTINGS", "notifyforecolor", m_appTheme.notifyfore);
    m_appTheme.hilite = set.readColorEntry("SETTINGS", "hilitecolor", m_appTheme.hilite);
    m_appTheme.shadow = set.readColorEntry("SETTINGS", "shadowcolor", m_appTheme.shadow);
    m_trayColor = set.readColorEntry("SETTINGS", "traycolor", m_appTheme.base);
    m_unreadColor = set.readColorEntry("SETTINGS", "unreadcolor", m_unreadColor);
    m_highlightColor = set.readColorEntry("SETTINGS", "highlightcolor", m_highlightColor);
    m_tabPosition = set.readIntEntry("SETTINGS", "tabPosition", m_tabPosition);
    m_font = set.readStringEntry("SETTINGS", "normalfont", m_font.text());

    //Irc Text settings pane in Configdialog
    m_colors.text = set.readColorEntry("SETTINGS", "textColor", m_colors.text);
    m_colors.back = set.readColorEntry("SETTINGS", "textBackColor", m_colors.back);
    m_colors.user = set.readColorEntry("SETTINGS", "userColor", m_colors.user);
    m_colors.action = set.readColorEntry("SETTINGS", "actionsColor", m_colors.action);
    m_colors.notice = set.readColorEntry("SETTINGS", "noticeColor", m_colors.notice);
    m_colors.error = set.readColorEntry("SETTINGS", "errorColor", m_colors.error);
    m_colors.hilight = set.readColorEntry("SETTINGS", "hilightColor", m_colors.hilight);
    m_colors.link = set.readColorEntry("SETTINGS", "linkColor", m_colors.link);
    m_colors.mymsg = set.readColorEntry("SETTINGS", "mymsgColor", m_colors.mymsg);
    m_colors.log = set.readColorEntry("SETTINGS", "logColor", m_colors.log);
    m_ircFont = set.readStringEntry("SETTINGS", "ircFont", m_ircFont.text());
    m_nickCompletionChar = FXString(set.readStringEntry("SETTINGS", "nickCompletionChar", m_nickCompletionChar.text())).left(1);
    m_sameCmd = set.readBoolEntry("SETTINGS", "sameCmd", m_sameCmd);
    m_sameList = set.readBoolEntry("SETTINGS", "sameList", m_sameList);
    m_coloredNick = set.readBoolEntry("SETTINGS", "coloredNick", m_coloredNick);
    m_stripColors = set.readBoolEntry("SETTINGS", "stripColors", m_stripColors);
    m_loadLog = set.readBoolEntry("SETTINGS", "loadLog", m_loadLog);
    m_loadLines = set.readIntEntry("SETTINGS", "loadLines", m_loadLines);

    //NickList settings pane in Configdialog
    m_themePath = utils::checkThemePath(set.readStringEntry("SETTINGS", "themePath", m_themePath.text()));
    m_themesList = utils::checkThemesList(set.readStringEntry("SETTINGS", "themesList", FXString(m_themePath+";").text()));
    if(m_themesList.before(';')!="internal") m_themesList.prepend("internal;");
    if(!m_themesList.contains(m_themePath)) m_themesList.append(m_themePath+";");
    m_sortUserMode = set.readBoolEntry("SETTINGS", "sortUserMode", m_sortUserMode);

    //Ignore settings pane in Configdialog
    m_ignoreCommandsList = set.readStringEntry("SETTINGS", "commandsList", m_ignoreCommandsList.text());
    FXint usersNum = set.readIntEntry("USERS", "number", 0);
    m_ignoreUsersList.clear();
    if(usersNum)
    {

        for(FXint i=0; i<usersNum; i++)
        {
            UserInfo user;
            user.nick = set.readStringEntry(FXStringFormat("USER%d", i).text(), "nick", FXStringFormat("xxx%d", i).text());
            user.channel = set.readStringEntry(FXStringFormat("USER%d", i).text(), "channel", "all");
            user.network = set.readStringEntry(FXStringFormat("USER%d", i).text(), "network", "all");
            m_ignoreUsersList.append(user);
        }
    }

    //DCC settings pane in Configdialog
    m_dccPath = set.readStringEntry("SETTINGS", "dccPath", m_dccPath.text());
    if(!FXStat::exists(m_dccPath)) m_dccPath = FXSystem::getHomeDirectory();
    m_dccIP = set.readStringEntry("SETTINGS", "dccIP", m_dccIP.text());
    FXRex rex("\\l");
    if(m_dccIP.contains('.')!=3 || rex.match(m_dccIP))
        m_dccIP = "";
    m_dccPortD = set.readIntEntry("SETTINGS", "dccPortD", m_dccPortD);
    m_dccPortH = set.readIntEntry("SETTINGS", "dccPortH", m_dccPortH);
    m_dccTimeout = set.readIntEntry("SETTINGS", "dccTimeout", m_dccTimeout);
    m_autoDccChat = set.readBoolEntry("SETTINGS", "autoDccChat", m_autoDccChat);
    m_autoDccFile = set.readBoolEntry("SETTINGS", "autoDccFile", m_autoDccFile);

    //Notification settings pane in Configdialog
    FXint friendsNum = set.readIntEntry("FRIENDS", "number", 0);
    m_friendsList.clear();
    if(friendsNum)
    {

        for(FXint i=0; i<friendsNum; i++)
        {
            UserInfo user;
            user.nick = set.readStringEntry(FXStringFormat("FRIEND%d", i).text(), "nick", FXStringFormat("xxx%d", i).text());
            user.channel = set.readStringEntry(FXStringFormat("FRIEND%d", i).text(), "channel", "all");
            user.network = set.readStringEntry(FXStringFormat("FRIEND%d", i).text(), "network", "all");
            m_friendsList.append(user);
        }
    }
    m_sounds = set.readBoolEntry("SETTINGS", "sounds", m_sounds);
    m_soundConnect = set.readBoolEntry("SETTINGS", "soundConnect", m_soundConnect);
    m_pathConnect = set.readStringEntry("SETTINGS", "pathConnect", m_pathConnect.text());
    m_soundDisconnect = set.readBoolEntry("SETTINGS", "soundDisconnect", m_soundDisconnect);
    m_pathDisconnect = set.readStringEntry("SETTINGS", "pathDisconnect", m_pathDisconnect.text());
    m_soundMessage = set.readBoolEntry("SETTINGS", "soundMessage", m_soundMessage);
    m_pathMessage = set.readStringEntry("SETTINGS", "pathMessage", m_pathMessage.text());
    m_notify = set.readBoolEntry("SETTINGS", "notify", m_notify);
    m_notifyPosition = set.readIntEntry("SETTINGS", "notifyPosition", m_notifyPosition);
    m_notifyConnect = set.readBoolEntry("SETTINGS", "notifyConnect", m_notifyConnect);
    m_notifyDisconnect = set.readBoolEntry("SETTINGS", "notifyDisconnect", m_notifyDisconnect);
    m_notifyMessage = set.readBoolEntry("SETTINGS", "notifyMessage", m_notifyMessage);

    //Smileys settings pane in Configdialog
    m_useSmileys = set.readBoolEntry("SETTINGS", "useSmileys", m_useSmileys);
    FXint smileysNum = set.readIntEntry("SMILEYS", "number", 0);
    m_smileysMap.clear();
    if(smileysNum)
    {

        for(FXint i=0; i<smileysNum; i++)
        {
            FXString key, value;
            key = set.readStringEntry("SMILEYS", FXStringFormat("smiley%d", i).text(), FXStringFormat("%d)", i).text());
            value = set.readStringEntry("SMILEYS", FXStringFormat("path%d", i).text(), "");
            if(!key.empty())
                m_smileysMap.insert(StringPair(key, value));
        }
    }

    //Other settings value
    FXint serversNum = set.readIntEntry("SERVERS", "number", 0);
    if(serversNum)
    {
        for(FXint i=0; i<serversNum; i++)
        {
            ServerInfo server;
            server.hostname = set.readStringEntry(FXStringFormat("SERVER%d", i).text(), "hostname", FXStringFormat("localhost%d", i).text());
            server.name = set.readStringEntry(FXStringFormat("SERVER%d", i).text(), "name", server.hostname.text());
            server.port = set.readIntEntry(FXStringFormat("SERVER%d", i).text(), "port", 6667);
            server.nick = set.readStringEntry(FXStringFormat("SERVER%d", i).text(), "nick", "xxx");
            server.realname = set.readStringEntry(FXStringFormat("SERVER%d", i).text(), "realname", "xxx");
            server.passwd = utils::decrypt(set.readStringEntry(FXStringFormat("SERVER%d", i).text(), "hes", ""));
            server.channels = set.readStringEntry(FXStringFormat("SERVER%d", i).text(), "channels", "");
            server.commands = set.readStringEntry(FXStringFormat("SERVER%d", i).text(), "commands", "");
            server.autoConnect = set.readBoolEntry(FXStringFormat("SERVER%d", i).text(), "autoconnect", FALSE);
            server.useSsl = set.readBoolEntry(FXStringFormat("SERVER%d", i).text(), "ssl", FALSE);
            m_serverList.append(server);
        }
    }
    m_usersShown = set.readBoolEntry("SETTINGS", "usersShown", m_usersShown);
    m_statusShown = set.readBoolEntry("SETTINGS", "statusShown", m_statusShown);
    m_x=set.readIntEntry("SETTINGS","x", m_x);
    m_y=set.readIntEntry("SETTINGS","y", m_y);
    m_w=set.readIntEntry("SETTINGS","w", m_w);
    m_h=set.readIntEntry("SETTINGS","h", m_h);
    m_maximized = set.readBoolEntry("SETTINGS", "maximized", m_maximized);
    m_showSpellCombo = set.readBoolEntry("SETTINGS", "showSpellCombo", m_showSpellCombo);
    FXint langsNum = set.readIntEntry("LANGS", "number", 0);
    m_langs.clear();
    if(langsNum)
    {

        for(FXint i=0; i<langsNum; i++)
        {
            FXString key, value;
            key = set.readStringEntry("LANGS", FXStringFormat("tab%d", i).text());
            value = set.readStringEntry("LANGS", FXStringFormat("lang%d", i).text());
            if(!key.empty() && !value.empty())
                m_langs.insert(StringPair(key, value));
        }
    }
    if((FXint)m_defaultkeybindings.size())
    {
        StringIt it;
        for(it=m_defaultkeybindings.begin(); it!=m_defaultkeybindings.end(); it++)
            m_keybindings.insert(StringPair((*it).first, set.readStringEntry("KEYBINDINGS", (*it).first.text(), (*it).second.text())));
    }

}

void Preferences::save()
{
    FXSettings set;
    //set.clear();

    //General settings pane in Configdialog
    set.writeBoolEntry("SETTINGS", "oneinstance", m_oneInstance);
    set.writeBoolEntry("SETTINGS", "disableSortTab", m_disableSortTabs);
    set.writeBoolEntry("SETTINGS", "reconnect", m_reconnect);
    set.writeIntEntry("SETTINGS", "numberAttempt", m_numberAttempt);
    set.writeIntEntry("SETTINGS", "delayAttempt", m_delayAttempt);
    set.writeIntEntry("SETTINGS", "maxAway", m_maxAway);
    set.writeBoolEntry("SETTINGS", "tray", m_useTray);
    set.writeBoolEntry("SETTINGS", "closeToTray", m_closeToTray);
    set.writeBoolEntry("SETTINGS", "useSpell", m_useSpell);
    set.writeBoolEntry("SETTINGS", "serverWindow", m_ownServerWindow);
    set.writeBoolEntry("SETTINGS", "logging", m_logging);
    set.writeStringEntry("SETTINGS", "logPath", m_logPath.text());
    set.writeBoolEntry("SETTINGS", "autoload", m_autoload);
    set.writeStringEntry("SETTINGS", "autoloadPath", m_autoloadPath.text());

    //Look settings pane in Configdialog
    set.writeColorEntry("SETTINGS", "basecolor", m_appTheme.base);
    set.writeColorEntry("SETTINGS", "backcolor", m_appTheme.back);
    set.writeColorEntry("SETTINGS", "bordercolor", m_appTheme.border);
    set.writeColorEntry("SETTINGS", "forecolor", m_appTheme.fore);
    set.writeColorEntry("SETTINGS", "selmenubackcolor", m_appTheme.menuback);
    set.writeColorEntry("SETTINGS", "selmenutextcolor", m_appTheme.menufore);
    set.writeColorEntry("SETTINGS", "selbackcolor", m_appTheme.selback);
    set.writeColorEntry("SETTINGS", "selforecolor", m_appTheme.selfore);
    set.writeColorEntry("SETTINGS", "tipbackcolor", m_appTheme.tipback);
    set.writeColorEntry("SETTINGS", "tipforecolor", m_appTheme.tipfore);
    set.writeColorEntry("SETTINGS", "notifybackcolor", m_appTheme.notifyback);
    set.writeColorEntry("SETTINGS", "notifyforecolor", m_appTheme.notifyfore);
    set.writeColorEntry("SETTINGS", "hilitecolor", m_appTheme.hilite);
    set.writeColorEntry("SETTINGS", "shadowcolor", m_appTheme.shadow);
    set.writeColorEntry("SETTINGS", "traycolor", m_trayColor);
    set.writeColorEntry("SETTINGS", "unreadcolor", m_unreadColor);
    set.writeColorEntry("SETTINGS", "highlightcolor", m_highlightColor);
    set.writeIntEntry("SETTINGS", "tabPosition", m_tabPosition);
    set.writeStringEntry("SETTINGS", "normalfont", m_font.text());

    //Irc Text settings pane in Configdialog
    set.writeColorEntry("SETTINGS", "textColor", m_colors.text);
    set.writeColorEntry("SETTINGS", "textBackColor", m_colors.back);
    set.writeColorEntry("SETTINGS", "userColor", m_colors.user);
    set.writeColorEntry("SETTINGS", "actionsColor", m_colors.action);
    set.writeColorEntry("SETTINGS", "noticeColor", m_colors.notice);
    set.writeColorEntry("SETTINGS", "errorColor", m_colors.error);
    set.writeColorEntry("SETTINGS", "hilightColor", m_colors.hilight);
    set.writeColorEntry("SETTINGS", "linkColor", m_colors.link);
    set.writeColorEntry("SETTINGS", "mymsgColor", m_colors.mymsg);
    set.writeColorEntry("SETTINGS", "logColor", m_colors.log);
    set.writeStringEntry("SETTINGS", "ircFont", m_ircFont.text());
    set.writeStringEntry("SETTINGS", "nickCompletionChar", m_nickCompletionChar.left(1).text());
    set.writeBoolEntry("SETTINGS", "sameCmd", m_sameCmd);
    set.writeBoolEntry("SETTINGS", "sameList", m_sameList);
    set.writeBoolEntry("SETTINGS", "coloredNick", m_coloredNick);
    set.writeBoolEntry("SETTINGS", "stripColors", m_stripColors);
    set.writeBoolEntry("SETTINGS", "loadLog", m_loadLog);
    set.writeIntEntry("SETTINGS", "loadLines", m_loadLines);

    //NickList settings pane in Configdialog
    set.writeStringEntry("SETTINGS", "themePath", m_themePath.text());
    set.writeStringEntry("SETTINGS", "themesList", m_themesList.text());
    set.writeBoolEntry("SETTINGS", "sortUserMode", m_sortUserMode);

    //Ignore settings pane in Configdialog
    set.writeStringEntry("SETTINGS", "commandsList", m_ignoreCommandsList.text());
    set.writeIntEntry("USERS", "number", m_ignoreUsersList.no());
    if(m_ignoreUsersList.no())
    {
        for(FXint i=0; i<m_ignoreUsersList.no(); i++)
        {
            set.writeStringEntry(FXStringFormat("USER%d", i).text(), "nick", m_ignoreUsersList[i].nick.text());
            set.writeStringEntry(FXStringFormat("USER%d", i).text(), "channel", m_ignoreUsersList[i].channel.text());
            set.writeStringEntry(FXStringFormat("USER%d", i).text(), "network", m_ignoreUsersList[i].network.text());
        }
    }

    //DCC settings pane in Configdialog
    set.writeStringEntry("SETTINGS", "dccPath", m_dccPath.text());
    set.writeStringEntry("SETTINGS", "dccIP", m_dccIP.text());
    set.writeIntEntry("SETTINGS", "dccPortD", m_dccPortD);
    set.writeIntEntry("SETTINGS", "dccPortH", m_dccPortH);
    set.writeIntEntry("SETTINGS", "dccTimeout", m_dccTimeout);
    set.writeBoolEntry("SETTINGS", "autoDccChat", m_autoDccChat);
    set.writeBoolEntry("SETTINGS", "autoDccFile", m_autoDccFile);

    //Notification settings pane in Configdialog
    set.writeIntEntry("FRIENDS", "number", m_friendsList.no());
    if(m_friendsList.no())
    {

        for(FXint i=0; i<m_friendsList.no(); i++)
        {
            set.writeStringEntry(FXStringFormat("FRIEND%d", i).text(), "nick", m_friendsList[i].nick.text());
            set.writeStringEntry(FXStringFormat("FRIEND%d", i).text(), "channel", m_friendsList[i].channel.text());
            set.writeStringEntry(FXStringFormat("FRIEND%d", i).text(), "network", m_friendsList[i].network.text());
        }
    }
    set.writeBoolEntry("SETTINGS", "sounds", m_sounds);
    set.writeBoolEntry("SETTINGS", "soundConnect", m_soundConnect);
    set.writeStringEntry("SETTINGS", "pathConnect", m_pathConnect.text());
    set.writeBoolEntry("SETTINGS", "soundDisconnect", m_soundDisconnect);
    set.writeStringEntry("SETTINGS", "pathDisconnect", m_pathDisconnect.text());
    set.writeBoolEntry("SETTINGS", "soundMessage", m_soundMessage);
    set.writeStringEntry("SETTINGS", "pathMessage", m_pathMessage.text());
    set.writeBoolEntry("SETTINGS", "notify", m_notify);
    set.writeIntEntry("SETTINGS", "notifyPosition", m_notifyPosition);
    set.writeBoolEntry("SETTINGS", "notifyConnect", m_notifyConnect);
    set.writeBoolEntry("SETTINGS", "notifyDisconnect", m_notifyDisconnect);
    set.writeBoolEntry("SETTINGS", "notifyMessage", m_notifyMessage);

    //Smileys settings pane in Configdialog
    set.writeBoolEntry("SETTINGS", "useSmileys", m_useSmileys);
    set.writeIntEntry("SMILEYS", "number", (FXint)m_smileysMap.size());
    if((FXint)m_smileysMap.size())
    {
        StringIt it;
        FXint i;
        for(i=0, it=m_smileysMap.begin(); it!=m_smileysMap.end(); it++,i++)
        {
            set.writeStringEntry("SMILEYS", FXStringFormat("smiley%d", i).text(), (*it).first.text());
            set.writeStringEntry("SMILEYS", FXStringFormat("path%d", i).text(), (*it).second.text());
        }
    }

    //Other settings value
    set.writeIntEntry("SERVERS", "number", m_serverList.no());
    if(m_serverList.no())
    {
        for(FXint i=0; i<m_serverList.no(); i++)
        {
            set.writeStringEntry(FXStringFormat("SERVER%d", i).text(), "name", m_serverList[i].name.text());
            set.writeStringEntry(FXStringFormat("SERVER%d", i).text(), "hostname", m_serverList[i].hostname.text());
            set.writeIntEntry(FXStringFormat("SERVER%d", i).text(), "port", m_serverList[i].port);
            set.writeStringEntry(FXStringFormat("SERVER%d", i).text(), "nick", m_serverList[i].nick.text());
            set.writeStringEntry(FXStringFormat("SERVER%d", i).text(), "realname", m_serverList[i].realname.text());
            set.writeStringEntry(FXStringFormat("SERVER%d", i).text(), "hes", utils::encrypt(m_serverList[i].passwd).text());
            set.writeStringEntry(FXStringFormat("SERVER%d", i).text(), "channels", m_serverList[i].channels.text());
            set.writeStringEntry(FXStringFormat("SERVER%d", i).text(), "commands", m_serverList[i].commands.text());
            set.writeBoolEntry(FXStringFormat("SERVER%d", i).text(), "autoconnect", m_serverList[i].autoConnect);
            set.writeBoolEntry(FXStringFormat("SERVER%d", i).text(), "ssl", m_serverList[i].useSsl);
        }
    }
    set.writeBoolEntry("SETTINGS", "usersShown", m_usersShown);
    set.writeBoolEntry("SETTINGS", "statusShown", m_statusShown);
    set.writeIntEntry("SETTINGS","x", m_x);
    set.writeIntEntry("SETTINGS","y", m_y);
    set.writeIntEntry("SETTINGS","w", m_w);
    set.writeIntEntry("SETTINGS","h", m_h);
    set.writeBoolEntry("SETTINGS", "maximized", m_maximized);
    set.writeBoolEntry("SETTINGS", "showSpellCombo", m_showSpellCombo);
    set.writeIntEntry("LANGS", "number", (FXint)m_langs.size());
    if((FXint)m_langs.size())
    {
        StringIt it;
        FXint i;
        for(i=0, it=m_langs.begin(); it!=m_langs.end(); it++,i++)
        {
            set.writeStringEntry("LANGS", FXStringFormat("tab%d", i).text(), (*it).first.text());
            set.writeStringEntry("LANGS", FXStringFormat("lang%d", i).text(), (*it).second.text());
        }
    }
    dxStringMap aliases = utils::instance().getAliases();
    set.writeIntEntry("ALIASES", "number", (FXint)aliases.size());
    if((FXint)aliases.size())
    {
        StringIt it;
        FXint i;
        for(i=0,it=aliases.begin(); it!=aliases.end(); it++,i++)
        {
            set.writeStringEntry("ALIASES", FXStringFormat("key%d", i).text(), (*it).first.text());
            set.writeStringEntry("ALIASES", FXStringFormat("value%d", i).text(), (*it).second.text());
        }
    }
    if((FXint)m_keybindings.size())
    {
        StringIt it;
        for(it=m_keybindings.begin(); it!=m_keybindings.end(); it++)
            set.writeStringEntry("KEYBINDINGS", (*it).first.text(), (*it).second.text());
    }
    set.setModified();
    set.unparseFile(utils::instance().getIniFile());
}

void Preferences::fillDefaultKeybindings()
{
    m_defaultkeybindings.clear();
    m_namekeybindings.clear();
    m_defaultkeybindings.insert(StringPair("serverList", "F2"));
    m_namekeybindings.insert(StringPair("serverList", _("Server list")));
    m_defaultkeybindings.insert(StringPair("quickConnect", "F7"));
    m_namekeybindings.insert(StringPair("quickConnect", _("Quick connect")));
    m_defaultkeybindings.insert(StringPair("disconnect", "Ctrl-D"));
    m_namekeybindings.insert(StringPair("disconnect", _("Disconnect")));
    m_defaultkeybindings.insert(StringPair("dccTransfers", "Ctrl-T"));
    m_namekeybindings.insert(StringPair("dccTransfers", _("DCC transfers")));
    m_defaultkeybindings.insert(StringPair("scripts", "Ctrl-S"));
    m_namekeybindings.insert(StringPair("scripts", _("Scripts")));
    m_defaultkeybindings.insert(StringPair("logViewer", "Ctrl-G"));
    m_namekeybindings.insert(StringPair("logViewer", _("Log viewer")));
#ifdef WIN32
    m_defaultkeybindings.insert(StringPair("quit", "Alt-F4"));
#else
    m_defaultkeybindings.insert(StringPair("quit", "Ctrl-Q"));
#endif //WIN32
    m_namekeybindings.insert(StringPair("quit", _("Quit")));
    m_defaultkeybindings.insert(StringPair("closeCurrentTab", "Ctrl-W"));
    m_namekeybindings.insert(StringPair("closeCurrentTab", _("Close current tab")));
    m_defaultkeybindings.insert(StringPair("clearWindow", "Ctrl-L"));
    m_namekeybindings.insert(StringPair("clearWindow", _("Clear window")));
    m_defaultkeybindings.insert(StringPair("clearAllWindows", "Ctrl-Shift-L"));
    m_namekeybindings.insert(StringPair("clearAllWindows", _("Clear all windows")));
    m_defaultkeybindings.insert(StringPair("usersList", "Ctrl-U"));
    m_namekeybindings.insert(StringPair("usersList", _("Users list")));
    m_defaultkeybindings.insert(StringPair("spellList", "Ctrl-P"));
    m_namekeybindings.insert(StringPair("spellList", _("Spellchecking language list")));
    m_defaultkeybindings.insert(StringPair("help", "F1"));
    m_namekeybindings.insert(StringPair("help", _("Help")));
    m_defaultkeybindings.insert(StringPair("switchTab", "Ctrl-Tab"));
    m_namekeybindings.insert(StringPair("switchTab", _("Switch between tabs")));
    m_defaultkeybindings.insert(StringPair("unreadTab", "Ctrl-N"));
    m_namekeybindings.insert(StringPair("unreadTab", _("Switch between unreaded tabs")));
    m_defaultkeybindings.insert(StringPair("moveTabUp", "Ctrl-Up"));
    m_namekeybindings.insert(StringPair("moveTabUp", _("Move tab up")));
    m_defaultkeybindings.insert(StringPair("moveTabDown", "Ctrl-Down"));
    m_namekeybindings.insert(StringPair("moveTabDown", _("Move tab down")));
    m_defaultkeybindings.insert(StringPair("forceFocus", "Ctrl-F"));
    m_namekeybindings.insert(StringPair("forceFocus", _("Force commandline focus in current tab")));
    m_defaultkeybindings.insert(StringPair("boldControlChar", "Ctrl-B"));
    m_namekeybindings.insert(StringPair("boldControlChar", _("Insert bold control char")));
    m_defaultkeybindings.insert(StringPair("colorControlChar", "Ctrl-K"));
    m_namekeybindings.insert(StringPair("colorControlChar", _("Insert color control char")));
    m_defaultkeybindings.insert(StringPair("underlineControlChar", "Ctrl-I"));
    m_namekeybindings.insert(StringPair("underlineControlChar", _("Insert underline control char")));
    m_defaultkeybindings.insert(StringPair("reverseControlChar", "Ctrl-R"));
    m_namekeybindings.insert(StringPair("reverseControlChar", _("Insert reverse control char")));
    m_defaultkeybindings.insert(StringPair("resetControlChar", "Ctrl-O"));
    m_namekeybindings.insert(StringPair("resetControlChar", _("Insert reset control char")));
    m_defaultkeybindings.insert(StringPair("firstTab", "Alt-1"));
    m_namekeybindings.insert(StringPair("firstTab", _("Go to 1. tab")));
    m_defaultkeybindings.insert(StringPair("secondTab", "Alt-2"));
    m_namekeybindings.insert(StringPair("secondTab", _("Go to 2. tab")));
    m_defaultkeybindings.insert(StringPair("thirdTab", "Alt-3"));
    m_namekeybindings.insert(StringPair("thirdTab", _("Go to 3. tab")));
    m_defaultkeybindings.insert(StringPair("fourthTab", "Alt-4"));
    m_namekeybindings.insert(StringPair("fourthTab", _("Go to 4. tab")));
    m_defaultkeybindings.insert(StringPair("fifthTab", "Alt-5"));
    m_namekeybindings.insert(StringPair("fifthTab", _("Go to 5. tab")));
    m_defaultkeybindings.insert(StringPair("sixthTab", "Alt-6"));
    m_namekeybindings.insert(StringPair("sixthTab", _("Go to 6. tab")));
    m_defaultkeybindings.insert(StringPair("seventhTab", "Alt-7"));
    m_namekeybindings.insert(StringPair("seventhTab", _("Go to 7. tab")));
    m_defaultkeybindings.insert(StringPair("eighthTab", "Alt-8"));
    m_namekeybindings.insert(StringPair("eighthTab", _("Go to 8. tab")));
    m_defaultkeybindings.insert(StringPair("ninethTab", "Alt-9"));
    m_namekeybindings.insert(StringPair("ninethTab", _("Go to 9. tab")));
}

FXString Preferences::getBinding(const FXString& key)
{
    if(m_keybindings.find(key)==m_keybindings.end())
        return "";
    else
        return m_keybindings.find(key)->second;
}

FXString Preferences::getBindingName(const FXString& key)
{
    if(m_namekeybindings.find(key)==m_namekeybindings.end())
        return "";
    else
        return m_namekeybindings.find(key)->second;
}


